package com.ayf.payment.game.api.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 
 */
public class DateTimeUtil {
	/**
	 * 
	 */
	private DateTimeUtil() {
	}

	/**
	 * 一秒钟的毫秒表示
	 */
	public final static long SECOND = 1000;
	/**
	 * 一分钟的毫秒表示
	 */
	public final static long MINUTE = 60L * SECOND;
	/**
	 * 一小时的毫秒表示
	 */
	public final static long HOUR = 60L * MINUTE;
	/**
	 * 一天的毫秒表示
	 */
	public final static long DAY = 24L * HOUR;
	/**
	 * 一周的毫秒表示
	 */
	public final static long WEEK = 7L * DAY;

	/**
	 * 一个月的毫秒表示(按30天计算)
	 */
	public final static long MONTH = 30L * DAY;
	/**
	 * 一年的毫秒表示(按365天计算)
	 */
	public final static long YEAR = 365L * DAY;

	/**
	 * 用户友好的日期时间格式:yyyy-MM-dd HH:mm:ss
	 */
	public final static String FRIENDLY_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 返回固定格式，精确到分钟
	 */
	private final static String ONLY_MINUTES_FORMAT = "yyyy-MM-dd HH:mm";
	
	/**
	 * 返回固定格式，精确到分钟
	 */
	private final static String MINUTES_FORMAT = "yyyy年MM月dd日HH时mm分";


	/**
	 * 返回固定格式，精确到秒
	 */
	public final static String TIME_FORMAT = "HH:mm:ss";

	/**
	 * 返回固定格式，精确到分钟
	 */
	private final static String ONLY_TIME_FORMAT = "HH:mm";

	/**
	 * 返回天日期
	 */
	private final static String YEAR_MONTH_DAY_FORMAT = "yyyy-MM-dd";

	/**
	 * 返回月天日期
	 */
	private final static String MONTH_DAY_TIME_FORMAT = "MM-dd HH:mm";




	/**
	 * 获取指定时间的yyyy-MM-dd HH:mm:ss表示格式
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String getDayString(long timestamp) {
		return new SimpleDateFormat(YEAR_MONTH_DAY_FORMAT).format(timestamp);
	}

	/**
	 * 获取指定时间MM-dd HH:mm 格式
	 * 
	 * @param timestamp
	 * @return String author luowei
	 */
	public static String getMonthDayTimeString(long timestamp) {
		return new SimpleDateFormat(MONTH_DAY_TIME_FORMAT).format(timestamp);
	}

	/**
	 * 获取指定时间的yyyy-MM-dd HH:mm:ss表示格式
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String getTimeString(long timestamp) {
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		cal.setTimeInMillis(timestamp);
		return new SimpleDateFormat(FRIENDLY_DATE_TIME_FORMAT).format(cal
				.getTimeInMillis());
	}

	/**
	 * 返回时间格式为yyyy-MM-dd HH:mm
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String getTimeMinutes(long timestamp) {
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		cal.setTimeInMillis(timestamp);
		return new SimpleDateFormat(ONLY_MINUTES_FORMAT).format(cal
				.getTimeInMillis());
	}

	/**
	 * 返回时间格式为yyyy-MM-dd HH:mm
	 * 
	 * @param timestamp
	 * @return
	 */
	public static String getMinutes(long timestamp) {
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		cal.setTimeInMillis(timestamp);
		return new SimpleDateFormat(MINUTES_FORMAT).format(cal.getTimeInMillis());
	}

	/**
	 * 返回时间格式为yyyy-MM-dd HH:mm
	 * 
	 * @param time
	 * @return
	 */
	public static String getTimeMinutes(Date time) {
		if (time == null)
			return "";
		return getTimeMinutes(time.getTime());
	}

	/**
	 * 获取当前的时间
	 * 
	 * @return
	 */
	public static String nowDate() {
		// DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return new SimpleDateFormat(FRIENDLY_DATE_TIME_FORMAT)
				.format(new Date());
	}

	/**
	 * 获取当前的时间(只有时间)
	 * 
	 * @return
	 */
	public static String nowDateOnlyTime() {
		// DateFormat format=new SimpleDateFormat("HH:mm:ss");
		return new SimpleDateFormat(TIME_FORMAT).format(new Date());
	}

	/**
	 * @param parseStr  HH:mm:ss
	 * @return
	 */
	public static Date parseTime(String parseStr){
		try {
			return new SimpleDateFormat(TIME_FORMAT).parse(parseStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	/**
	 * 获取指定日期的时间(HH:mm)
	 * 
	 * @return
	 */
	public static String onlyTime(Date date) {
		return new SimpleDateFormat(ONLY_TIME_FORMAT).format(date);
	}

	/**
	 * 获取指定日期的时间(HH:mm)
	 * 
	 * @return
	 */
	public static String onlyTime(long date) {
		return new SimpleDateFormat(ONLY_TIME_FORMAT).format(date);
	}

	/**
	 * 通过指定日历获取年份
	 * 
	 * @param cal
	 * @return
	 */
	public static int getYear(Calendar cal) {
		return cal.get(Calendar.YEAR);
	}

	/**
	 * 通过指定日历获取月份(1-12)
	 * 
	 * @param cal
	 * @return 1月到12月分别返回1-12
	 */
	public static int getMonth(Calendar cal) {
		return cal.get(Calendar.MONTH) + 1;
	}

	/**
	 * 通过指定日历获取天
	 * 
	 * @param cal
	 * @return
	 */
	public static int getDayOfMonth(Calendar cal) {
		return cal.get(Calendar.DAY_OF_MONTH);
	}
	
	public static int getDayOfYear(Calendar cal) {
		return cal.get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 通过指定日历获取星期
	 * 
	 * @param cal
	 * @return 星期一到星期天分别对应1-7
	 */
	public static int getDayOfWeek(Calendar cal) {
		return cal.get(Calendar.DAY_OF_WEEK) - 1;
	}

	/**
	 * 获取小时(24小时制)
	 * 
	 * @param cal
	 * @return
	 */
	public static int getHourOfDay(Calendar cal) {
		return cal.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 获取分钟
	 * 
	 * @param cal
	 * @return
	 */
	public static int getMinute(Calendar cal) {
		return cal.get(Calendar.MINUTE);
	}

	/**
	 * 获取秒
	 * 
	 * @param cal
	 * @return
	 */
	public static int getSecond(Calendar cal) {
		return cal.get(Calendar.SECOND);
	}

	/**
	 * 返回当前的小时数(24小时 0-23)
	 * 
	 * @return
	 */
	public static int getCurrentHour() {
		return getHourOfDay(Calendar.getInstance());
	}

	/**
	 * 保留date1的日期部分，保留Date2的时间部分
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static Date changeTime(Date date1, Date date2) {
		return new Date(date1.getTime() / DAY * DAY + date2.getTime() % DAY);
	}

	/**
	 * 比较两个日期的时间部分是否一致
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean compareTime(Date date1, Date date2) {
		return date1.getTime() % DAY == date2.getTime() % DAY;
	}

	/**
	 * 比较两个Date日期部分的大小
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int compareDate(Date date1, Date date2) {
		if (date1.getTime() / DAY == date2.getTime() / DAY) {
			return 0;
		} else if (date1.getTime() / DAY > date2.getTime() / DAY) {
			return 1;
		} else {
			return -1;
		}
	}

	/**
	 * 根据输入日期的时间，获得下一次运行的时间的的毫秒数
	 * 
	 * @param date
	 * @return
	 */
	public static long dayRunTime(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Calendar runTime = Calendar.getInstance();
		runTime.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
		runTime.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
		runTime.set(Calendar.SECOND, cal.get(Calendar.SECOND));
		return runTime.getTimeInMillis();
	}

	/**
	 * 如果是以周为周期进行循环，计算下一次启动的时间
	 * 
	 * @param date
	 * @param dayOfWeek
	 *            从1开始到7
	 * @return
	 */
	public static long weekRunTime(Date date, byte dayOfWeek) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Calendar runTime = Calendar.getInstance();
		if ((dayOfWeek + 8) % 7 >= runTime.get(Calendar.DAY_OF_WEEK)) {
			runTime.add(Calendar.DATE,
					(dayOfWeek + 8) % 7 - runTime.get(Calendar.DAY_OF_WEEK));
		} else {
			runTime.add(Calendar.DATE,
					(dayOfWeek + 8) % 7 - runTime.get(Calendar.DAY_OF_WEEK) + 7);
		}
		runTime.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
		runTime.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
		runTime.set(Calendar.SECOND, cal.get(Calendar.SECOND));
		return runTime.getTimeInMillis();
	}

	/**
	 * 如果以月为周期启动，计算下一次启动时间
	 * 
	 * @param date
	 * @param dayOfMonth
	 * @return
	 */
	public static long monthRunTime(Date date, byte dayOfMonth) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		Calendar runTime = Calendar.getInstance();
		if (dayOfMonth >= runTime.get(Calendar.DAY_OF_MONTH)) {
			runTime.add(Calendar.DATE,
					dayOfMonth - runTime.get(Calendar.DAY_OF_MONTH));
		} else {
			runTime.add(Calendar.MONTH, 1);
			runTime.set(Calendar.DATE, dayOfMonth);
		}
		runTime.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY));
		runTime.set(Calendar.MINUTE, cal.get(Calendar.MINUTE));
		runTime.set(Calendar.SECOND, cal.get(Calendar.SECOND));
		return runTime.getTimeInMillis();
	}

	/**
	 * 将指定的时间添加指定的天数
	 * 
	 * @param date
	 * @param day
	 * @return
	 */
	public static Date addDay(Date date, int day) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, day);
		return cal.getTime();
	}

	public static Date addDay(int day) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, day);
		return cal.getTime();
	}

	/**
	 * 给指定的时间添加指定的小时
	 * 
	 * @param date
	 * @param hour
	 * @return
	 */
	public static Date addHour(Date date, int hour) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, hour);
		return cal.getTime();
	}

	public static Date addMinute(int minute) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MINUTE, minute);
		return cal.getTime();
	}

	/**
	 * @param date
	 * @param minute
	 * @return
	 * @author ChenYongcun
	 * @description 给指定时间增加指定分钟数
	 */
	public static Date addMinute(Date date, int minute) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, minute);
		return cal.getTime();
	}

	public static Timestamp string2Timestamp(String time) {
		Timestamp date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date d = sdf.parse(time);
			date = new Timestamp(d.getTime());
		} catch (Exception e) {
		}
		return date;
	}

	public static Date string2Date(String time) {
		Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			date = sdf.parse(time);
		} catch (Exception e) {
			date = new Date();
		}
		return date;
	}

	public static String time2String(Date time) {
		String timeString = null;
		try {
			// SimpleDateFormat sdf = new
			// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			timeString = new SimpleDateFormat(FRIENDLY_DATE_TIME_FORMAT)
					.format(time);
		} catch (Exception e) {
			// logger.error("时间转字符串出错", e);
		}
		return timeString;
	}

	public static String time2String(long time) {
		Date d = new Date();
		d.setTime(time);
		return time2String(d);
	}

	/**
	 * 获取整天日期
	 * 
	 * @param dayTime
	 * @param offset
	 * @return
	 */
	public static final Calendar getDayDate(Calendar dayTime, int offset) {
		Calendar calendar = Calendar.getInstance();
		if (dayTime != null)
			calendar.setTimeInMillis(dayTime.getTimeInMillis());

		calendar.add(Calendar.DAY_OF_YEAR, offset);

		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);

		return calendar;
	}

	/**
	 * 获取指定时间的日历
	 * 
	 * @param time
	 * @return
	 */
	public static final Calendar getCalendar(long time) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(time);

		return calendar;
	}

	/**
	 * 两个日期之间相隔天数的共通
	 * 
	 * @param startDate
	 *            开始时间
	 * @param endDate
	 *            　结束时间
	 * @return　天数
	 */
	public static int getDaysBetweenTwoDates(long startDate, long endDate) {
//		System.out.println("--- nowDay:" + (endDate / DateTimeUtil.DAY) + ";lastDay:" + (startDate / DateTimeUtil.DAY) + ";");
//		return (int) (endDate / DateTimeUtil.DAY - startDate / DateTimeUtil.DAY);
		
		
		Calendar calendar = getCalendar(startDate);
		int d = getDayOfYear(calendar);
		Calendar today = getCalendar(endDate);
		
		return Math.abs(d - getDayOfYear(today));
		
	}
	public static int getDaysBetweenTwoDates(Date startDate, Date endDate) {
//		long begin = startDate.getTime();
//		long end = endDate.getTime();
//		long inter = end - begin;
//		if (inter < 0) {
//			inter = inter * (-1);
//		}
//
//		long dateMillSec = 24 * 60 * 60 * 1000;
//		long dateCnt = inter / dateMillSec;
//		long remainder = inter % dateMillSec;
//
//		if (remainder != 0) {
//			dateCnt++;
//		}
//		return (int) dateCnt;
		
		if(endDate != null && startDate != null){
			
			return getDaysBetweenTwoDates(startDate.getTime(), endDate.getTime());
		}
		
		return 0;
	}

	public static boolean isToday(long time){
		
		Calendar calendar = getCalendar(time);
		int d = getDayOfYear(calendar);
		Calendar today = getCalendar(System.currentTimeMillis());
		
		if(getYear(calendar) == getYear(today)) {
			
			return d == getDayOfYear(today);
		}
		
		return false;
	}
	
	public static void main(String... args) throws Exception {
//		 Calendar calendar = Calendar.getInstance();
//		 
//		 calendar.setTimeInMillis(System.currentTimeMillis());
		 
		 
		 Calendar other = Calendar.getInstance();
		 
		 other.setTimeInMillis(System.currentTimeMillis());	
		 other.add(Calendar.YEAR, 1);
		 
		 
		 System.out.println(isToday(other.getTimeInMillis()));
		// calendar.set(Calendar.DAY_OF_MONTH, 4);
		// System.out.println(DateTimeUtil.getDaysBetweenTwoDates(calendar.getTime(),
		// new Date()));
		// System.out.println(DateTimeUtil.time2String(new
		// Date()).split(" ")[1]);
		// Date d = new Date();
		// Calendar cal = Calendar.getInstance();
		// cal.setFirstDayOfWeek(Calendar.MONDAY);
		//
		// System.err.println(cal.get(Calendar.YEAR));
		// System.err.println(cal.get(Calendar.MONTH) + 1);
		// System.err.println(cal.get(Calendar.DATE));
		// System.err.println(cal.get(Calendar.DAY_OF_WEEK) - 1);
		// System.err.println(cal.get(Calendar.HOUR_OF_DAY));
		// System.err.println(cal.get(Calendar.MINUTE));
		// System.err.println(cal.get(Calendar.SECOND));
		//
		//
		//
		// long now = System.currentTimeMillis();
		//
		// System.err.println(getTimeString(now));
		//
		// System.err.println(buildTimeString(now, true, true));
		// System.err.println(buildTimeString(YEAR + 12 * DAY + 10* MINUTE));
		// System.err.println(buildTimeString(DAY));
		// System.err.println(buildTimeString(HOUR));
		// System.err.println(buildTimeString(1200));
		// System.err.println(buildTimeString(200));
		// System.err.println(buildTimeString(0));
		// System.err.println(buildTimeString(-1));
		//
		// Date date=new Date();
		//
		// date.setTime(System.currentTimeMillis()-DateTimeUtil.DAY*5);
		// System.out.println(date);
		// System.out.println(new Date(DateTimeUtil.dayRunTime(date)));
		// System.out.println(new Date(DateTimeUtil.weekRunTime(date,(byte)3)));
		// System.out.println(new Date(DateTimeUtil.weekRunTime(date,(byte)7)));
		// System.out.println(new
		// Date(DateTimeUtil.monthRunTime(date,(byte)12)));
		// System.out.println(new
		// Date(DateTimeUtil.monthRunTime(date,(byte)30)));
		// System.out.println("------------------");
		// System.out.println(changeTime(new Date(),new
		// Date(System.currentTimeMillis()-10000)));
		//
		
	}

}
